package com.squirrel.michale;

import java.util.*;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/2/26 7:50 PM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode2212_2 {

    // 我计算了每枝箭的平均收益，排序后按收益决定箭如何分布。但是第一个测试用例都每通过，也不知道怎么改进。先放在这吧。以后知道怎么改进再说。
    public int[] maximumBobPoints(int numArrows, int[] aliceArrows) {
        int n = aliceArrows.length;
        List<AverageArrowScore> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int aliceArrow = aliceArrows[i];
            double score = i ;
            double  averageBob = score / (aliceArrow + 1);
            AverageArrowScore averageArrowScore = new AverageArrowScore(averageBob,i,aliceArrow+1);
            list.add(averageArrowScore);
        }
        Collections.sort(list, new Comparator<AverageArrowScore>() {
            @Override
            public int compare(AverageArrowScore o1, AverageArrowScore o2) {
                return o2.averageArrowScore.compareTo(o1.averageArrowScore);
            }
        });

        int[] bobArrows = new int[n];
        for (int i = 0; i < list.size(); i++) {
            AverageArrowScore averageArrowScore = list.get(i);
            bobArrows[averageArrowScore.totalScore] = averageArrowScore.arrowCount;
            numArrows -= averageArrowScore.arrowCount;
            if (numArrows == 0) {
                return bobArrows;
            } else if (numArrows < 0) {
                bobArrows[averageArrowScore.totalScore] = 0;
                numArrows += averageArrowScore.arrowCount;
            }
        }
        return bobArrows;
    }

    class AverageArrowScore{
        Double averageArrowScore;

        int totalScore;

        int arrowCount;

        public AverageArrowScore(double averageArrowScore, int totalScore, int arrowCount) {
            this.averageArrowScore = averageArrowScore;
            this.totalScore = totalScore;
            this.arrowCount = arrowCount;
        }
    }


    public static void main(String[] args) {
        LeetCode2212_2 leetCode2212 = new LeetCode2212_2();
        int numArrows = 9;
        int[] aliceArrows = new int[]{1, 1, 0, 1, 0, 0, 2, 1, 0, 1, 2, 0};

        System.out.println(Arrays.toString(leetCode2212.maximumBobPoints(numArrows, aliceArrows)));

//       ? 0,0,0,0,1,1,0,0,1,2,3,1]

        //[0, 0, 1, 0, 1, 1, 3, 2, 1, 2, 3, 1]

       // 0, 0, 1, 0, 1, 1, 0, 2, 1, 2, 0, 1
    }
}
